import {HttpHelper} from "./HttpHelper";
import { HttpClient } from '@angular/common/http';
import {Injectable} from "@angular/core";
import {Manufacture} from "./domains";

@Injectable()
export class ManufactureService{

    manufactures:[
        {
            id:1;
            manufactureName:'漆柜';
            notUsed:true;
            type:"FINISHED";
            price:500;
        },
        {
            id:1;
            manufactureName:'书桌';
            notUsed:true;
            type:"SEMI";
            price:300;
        }
        ]
    constructor(private httpHelper: HttpHelper,private httpClient: HttpClient){

    }
    manufactureUrl = "manufactures";


    getManufactures(page?,size?):Promise<Manufacture[]>{
        let url = this.manufactureUrl;
        if (page&&size) {
            url = url + `/?page=${page}&size=${size}`;
        }
        return this.httpHelper.getJson<Manufacture[]>(url);
    }

    addManufacture(manufacture:Manufacture):Promise<Manufacture>{
        return this.httpHelper.postToJson<Manufacture>(this.manufactureUrl,manufacture);
    }

    editManufacture(manufacture:Manufacture):Promise<boolean>{
        return this.httpHelper.putToBoolean(this.manufactureUrl,manufacture);
    }

    getManufacture(id):Promise<Manufacture> {
        let url = this.manufactureUrl+`/${id}`;
        return this.httpHelper.getJson<Manufacture>(url);
    }

    editManufactureMessage(manufacture:Manufacture):Promise<Manufacture> {
        return this.httpHelper.putToJson(this.manufactureUrl+"edit/message",manufacture);
    }
}